import mysql.connector


# 数据库参数
def config(param):
    data_base_config = {
        'host': '192.168.0.223',
        'user': 'web_home_py',
        'password': 'Lrx7BPAy362N4GBs',
        'database': 'web_home_py'
    }
    return data_base_config.get(param)


# 保存数据: result为插入语句集合
def save(result):
    if len(result) == 0:
        pass

    # 链接数据库
    db = mysql.connector.connect(host=config('host'), user=config('user'),
                                 password=config('password'), database=config('database'), charset='utf8')
    cursor = db.cursor()
    try:
        for insertSql in result:
            # 执行sql语句
            print('sql: %s' % insertSql)
            cursor.execute(insertSql)
        # 提交到数据库执行
        db.commit()
    except BaseException as e:
        # Rollback in case there is any error
        db.rollback()
        print('异常结果：', e)
    finally:
        # 关闭数据库连接
        db.close()


if __name__ == '__main__':
    aaa = []
    save(aaa)
